package com.ta.gulimall.product.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import com.ta.gulimall.common.constant.ProductConstant;
import com.ta.gulimall.common.enums.ProductAttrTypeEnums;
import com.ta.gulimall.common.utils.system.PageUtils;
import com.ta.gulimall.common.utils.system.Query;
import com.ta.gulimall.product.dao.AttrDao;
import com.ta.gulimall.product.service.AttrGroupService;
import com.ta.gulimall.product.service.CategoryService;
import com.ta.gulimall.product.view.entity.AttrAttrgroupRelationEntity;
import com.ta.gulimall.product.view.entity.AttrEntity;
import com.ta.gulimall.product.service.AttrAttrgroupRelationService;
import com.ta.gulimall.product.service.AttrService;
import com.ta.gulimall.product.view.entity.AttrGroupEntity;
import com.ta.gulimall.product.view.vo.AttrVo;
import com.ta.gulimall.product.view.vo.AttrVoResponse;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.stream.Collectors;


@Service("attrService")
@Slf4j
public class AttrServiceImpl extends ServiceImpl<AttrDao, AttrEntity> implements AttrService {
    @Autowired
    AttrAttrgroupRelationService attrAttrgroupRelationService;
    @Autowired
    CategoryService categoryService;
    @Autowired
    AttrGroupService attrGroupService;

    @Transactional
    @Override
    public void saveAttr(AttrVo attr) {
        AttrEntity attrEntity = BeanUtil.copyProperties(attr, AttrEntity.class);
        this.save(attrEntity);
        attr.setAttrId(attrEntity.getAttrId());
        attrAttrgroupRelationService.saveAttrAttrGroupRelation(attr);
    }

    @Transactional
    @Override
    public void updateAttr(AttrVo attr) {
        AttrEntity attrEntity = BeanUtil.copyProperties(attr, AttrEntity.class);
        this.updateById(attrEntity);
        attrAttrgroupRelationService.updateAttrAttrgroupRelation(attr);
    }


    @Override
    public PageUtils pageAttrs(Map<String, Object> params, Long catelogId, String attrType) {
        IPage<AttrEntity> page = this.basePageAttrs(params, catelogId, attrType);
        List<AttrVoResponse> attrVos = page.getRecords().stream().map(this::getAttrVoResponse).collect(Collectors.toList());
        return new PageUtils(page).setList(attrVos);
    }

    @Override
    public AttrVoResponse getAttr(Long attrId) {
        AttrEntity attr = getById(attrId);
        AttrVoResponse attrVoResponse = this.getAttrVoResponse(attr);
        attrVoResponse.setCatelogPath(categoryService.getClassifyPath(attr.getCatelogId()));
        return attrVoResponse;
    }

    @Override
    public IPage<AttrEntity> pageAttrs(Map<String, Object> params, Long catelogId) {

        return this.basePageAttrs(params, catelogId, "base");
    }

    @Override
    public List<AttrEntity> getAttrs(Long attrgroupId) {
        List<AttrAttrgroupRelationEntity> attrAttrgroupRelations = Optional.ofNullable(attrAttrgroupRelationService.getAttrAttrgroupRelations(attrgroupId)).orElse(new ArrayList<>());
        List<Long> attrIds = attrAttrgroupRelations.stream().map(AttrAttrgroupRelationEntity::getAttrId).collect(Collectors.toList());
        return this.listByIds(attrIds).stream().filter(f -> f.getAttrType().equals(ProductAttrTypeEnums.BASIC_ATTR.getCode())).collect(Collectors.toList());
    }

    @Override
    public List<Long> listAttrs(List<Long> attrIds) {
        LambdaQueryWrapper<AttrEntity> wrapper = new LambdaQueryWrapper<AttrEntity>()
                .in(AttrEntity::getAttrId, attrIds).eq(AttrEntity::getSearchType, Boolean.TRUE);
        attrIds = list(wrapper).stream().map(AttrEntity::getAttrId).collect(Collectors.toList());

        return attrIds;
    }

    private AttrVoResponse getAttrVoResponse(AttrEntity attr) {
        AttrVoResponse attrVoResponse = BeanUtil.copyProperties(attr, AttrVoResponse.class);
        if (ObjectUtil.isNotEmpty(attr.getCatelogId())) {
            String categoryName = categoryService.getById(attr.getCatelogId()).getName();
            attrVoResponse.setCatelogName(categoryName);
        }

        if (attr.getAttrType().equals(ProductAttrTypeEnums.MARKET_ATTR.getCode())) {
            return attrVoResponse;
        }

        AttrAttrgroupRelationEntity attrAttrgroupRelation = Optional.ofNullable(attrAttrgroupRelationService.getAttrAttrgroupRelation(attr.getAttrId())).orElse(new AttrAttrgroupRelationEntity());
        if (ObjectUtil.isNotEmpty(attrAttrgroupRelation.getAttrGroupId()) && !"0".equals(attrAttrgroupRelation.getAttrGroupId().toString())) {
            AttrGroupEntity attrGroup = attrGroupService.getById(attrAttrgroupRelation.getAttrGroupId());
            attrVoResponse.setGroupName(attrGroup.getAttrGroupName());
        }
        return attrVoResponse;
    }

    public  IPage<AttrEntity>  basePageAttrs(Map<String, Object> params, Long catelogId, String attrType) {
        String key = (String) params.get("key");
        LambdaQueryWrapper<AttrEntity> queryWrapper = new LambdaQueryWrapper<AttrEntity>()
                .eq(!ProductConstant.LEVEL1.equals(catelogId), AttrEntity::getCatelogId, catelogId)
                .and(StringUtils.isNotBlank(key),eq -> eq.eq(AttrEntity::getAttrId, key).or().like(AttrEntity::getAttrName, key))
                .eq(AttrEntity::getAttrType, "base".equals(attrType) ? ProductAttrTypeEnums.BASIC_ATTR.getCode() : ProductAttrTypeEnums.MARKET_ATTR.getCode());
        IPage<AttrEntity> page = page(new Query<AttrEntity>().getPage(params), queryWrapper);
        return page;
    }




}